package com.kedacom.freedomLabel.printer;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.kedacom.freedomLabel.model.bean.Business;
import com.kedacom.freedomLabel.utils.log.LLog;
import com.lzscale.scalelib.misclib.PrintStatusCallback;
import com.lzscale.scalelib.misclib.PrintStatusResult;
import com.lzscale.scalelib.misclib.SPrinter;

public class CustomPrinter extends ScalePrinter<Business> {

    private static final String SCALES_DOCUMENT = "销售凭证";
    private static final String DEAL_TIME = "交易时间:";
    private static final String DEAL_NUMBER = "订单号:";
    private static final String SCALE_ID = "摊位名称:";
    private static CustomPrinter mCustomPrinter = null;
    private static SPrinter mPrinter = null;

    private CustomPrinter() {
        mPrinter = new SPrinter();
    }

    public static CustomPrinter getInstance() {
        if (mCustomPrinter == null) {
            synchronized (CustomPrinter.class) {
                if (mCustomPrinter == null) {
                    try {
                        mCustomPrinter = new CustomPrinter();
                    } catch (Exception e) {
                        LLog.error("CustomPrinter", e.getMessage());
                    }
                }
            }
        }
        return mCustomPrinter;
    }

    @Override
    public void print(@NonNull Business oi, @Nullable PrintStatusCallback callback) {
        this.isReprint = false;
        this.go(oi, callback);
    }

    private void go(final Business info, final PrintStatusCallback callback) {
        printThread.submit(new Runnable() {
            @Override
            public void run() {
                try {

                    printVersionName();

                    if (callback != null)
                        callback.run(new PrintStatusResult(0, "success"));

                } catch (Exception e) {
                    if (callback != null)
                        callback.run(new PrintStatusResult(1, "failed:" + e.getMessage()));
                }
            }
        });
    }

    private void printVersionName() {
        mPrinter.print(getCenterStr("V1.0.0", false) + LINE_BREAK);
        mPrinter.print(LINE_BREAK);
        mPrinter.print(LINE_BREAK);
        mPrinter.print(LINE_BREAK);
    }
}